﻿//**********************************************************
//Copyright(C)2010 By 波特商网公司 版权所有。
//
//文件名：
//文件功能：
//
//创建标识： || 2011-04-01
//
//修改标识：
//修改描述：
//**********************************************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace Hmc.IM.Common.UserControls
{
    /// <summary>
    /// 
    /// </summary>
    public class ucTreeView : TreeView
    {

        private Color borderColor;

        /// <summary>
        /// 边框颜色
        /// </summary>
        public Color BorderColor
        {
            get { return borderColor; }
            set { borderColor = value; }
        }

        private int borderWidth = 1;

        /// <summary>
        /// 边框宽度
        /// </summary>
        public int BorderWidth
        {
            get { return borderWidth; }
            set { borderWidth = value; }
        }

        /// <summary>
        /// 重写消息事件
        /// </summary>
        /// <param name="m"></param>
        protected override void WndProc(ref Message m)
        {
            base.WndProc(ref m);
            if (m.Msg == NativeMethods.WM_PAINT ||
                m.Msg == NativeMethods.WM_CTLCOLOREDIT)
            {
                IntPtr hDC = NativeMethods.GetWindowDC(m.HWnd);
                if (hDC.ToInt32() == 0)
                {
                    return;
                }

                //只有在边框样式为FixedSingle时自定义边框样式才有效 
                if (this.BorderStyle == BorderStyle.None)
                {
                    //如果是必填项
                    using (Pen pen = new Pen(BorderColor, this.BorderWidth))
                    {
                        
                        //绘制边框 
                        System.Drawing.Graphics g = Graphics.FromHdc(hDC);
                        g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                        g.DrawRectangle(pen, 0, 0, this.Width - 1, this.Height - 1);
                    }
                }

                //返回结果 
                m.Result = IntPtr.Zero;
                //释放 
                NativeMethods.ReleaseDC(m.HWnd, hDC);
            }
        }
    }
}
